
import java.io.Serializable;
import java.util.InputMismatchException;
import java.util.Scanner;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author TQNINH
 */
public class Customer implements Serializable {
    String cusID;
    String name;
    byte gender;
    byte age;
    static int lastID = 0;
    
    public Customer() {
        lastID++;
        this.cusID = String.format("C%04d", lastID);
        this.name = null;
        this.gender = -1;
        this.age = 0;
    }
    
    public Customer(String i, String n, byte g, byte a) {
        this.cusID = i;
        this.name = n;
        this.gender = g;
        this.age = a;
    }
    
    @Override
    public String toString() {
        return "Customer ID: " + cusID + "\t" +
                "Name: " + name + "\t" +
                "Gender: " + CheckGender() + "\t" +
                "Age: " + age;
    }

    public void inputCustomer() {
        Scanner input = new Scanner(System.in);
        input.useDelimiter("\n");
        
        System.out.println("--- CUSTOMER INFORMATION ---");
        System.out.println("Customer ID: " + cusID);
        System.out.print("Enter customer's name: ");
        name = input.next();
        while(true) {
            try {
                System.out.print("Enter customer's age: ");
                age = input.nextByte();
                if(age < 8 || age > 100) {
                    throw new CheckAge();
                }
                break;
            } catch (Exception e) {
                System.out.println("Message: \"Enter age from 8-100!\"");
                input.nextLine();
            }
        }
        while(true) {
            try {
                System.out.println("Enter customer's gender: ");
                System.out.println("  1. Male\n"
                                    + "  2. Female\n"
                                    + "  3. Other");
                System.out.print("Gender: ");
                gender = input.nextByte();
                if(gender < 1 || gender > 3) {
                    throw new CheckSelectGender();
                }
                break;
            } catch (Exception e) {
                System.out.println("Message: \"Enter 1, 2 or 3!\"");
                input.nextLine();
            }
        }
        
    }
    
    public String CheckGender() {
        if(gender == 1) {
            return "Male";
        } else if (gender == 2) {
            return "Female";
        } else {
            return "Other";
        }
    }
}

class CheckSelectGender extends Exception {
    
}

class CheckAge extends Exception {
    
}
